package com.wg.user.controller;

import com.wg.user.model.User;
import com.wg.user.result.ResponseCode;
import com.wg.user.result.ResponseMsg;
import com.wg.user.result.ResponseResult;
import com.wg.user.service.UserService;
import com.wg.user.util.MyJwt;
import com.wg.user.util.MyMd5;
import com.wg.user.vo.UserVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;

/**
 * Created by mc on 18/8/8.
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController{

    @Autowired
    private UserService userService;

    @ApiOperation(value = "创建用户", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "账号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "note", value = "备注", required = false, dataType = "String", paramType = "query")
    })
    @PostMapping("/user/create")
    public ResponseResult<Long> create(@RequestParam(value = "name", required = true) String name,
                                 @RequestParam(value = "password", required = true) String password,
                                 @RequestParam(value = "note", required = false) String note) {

        User existsUser = userService.getByName(name);
        if (existsUser != null) {
            return ResponseResult.toFail( "用户名已存在");
        }
        User user = new User();
        user.setName(name);
        user.setPassword(MyMd5.get(password));
        user.setDeleted( 0);
        user.setNickName(name+System.currentTimeMillis());
        user.setCreated(new Date());
        user.setUpdated(new Date());
        Long id = userService.create(user);
        if(id != null){
            return ResponseResult.toSuccess(id);
        } else {
            return ResponseResult.toFail("创建用户失败");
        }
    }


    @ApiOperation(value = "用户登录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "账号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping("/login")
    public ResponseResult<UserVO> login(@RequestParam(value = "name", required = true) String name,
                                        @RequestParam(value = "password", required = true) String password) {
        User user = userService.getByName(name);
        if (user == null) {
            return ResponseResult.toFail( "没有找到用户");
        }
        if (!MyMd5.compare(password, user.getPassword())) {
            return ResponseResult.toFail(ResponseCode.WRONG_PASSWORD, ResponseMsg.WRONG_PASSWORD);
        }
        UserVO userDto = this.mapper.map(user, UserVO.class);
        try {
            HashMap<String, Long> userJwtMap = new HashMap<>(1);
            userJwtMap.put("id", userDto.getId());
            MyJwt myJwt = new MyJwt();
            userDto.setJwt(myJwt.generateJwt(userJwtMap));
            return ResponseResult.toSuccess(userDto);
        } catch (Exception e) {
            return ResponseResult.toFail(e.getMessage());
        }
    }
}
